package com.sztest.jayjun.dialog.BaseDialog;

/*

 */

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;


import com.sztest.jayjun.R;

import androidx.annotation.NonNull;


/**
 * 只有2个按钮的基类.类似 确认 取消
 * 一个 id 是 btn_ensure 为积极的
 * 一个id 是 btn_cancel 为消极的
 */
public abstract class Base2Dialog extends Dialog {
    protected onEnsureOrCancelListener mListener;

    public Base2Dialog(@NonNull Context context) {
        this(context, R.style.dialog);
    }

    public Base2Dialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setSystemUIVisible(false);
        super.onCreate(savedInstanceState);
        initContentViewBefor();
        setContentView(getlayoutId());
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
        attributes.height = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(attributes);
        initView();
        initData();
    }

    protected void initData() {

    }

    protected void initView() {
        View vensure = findViewById(R.id.btn_ensure);

        if (vensure!=null) {
            vensure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    if (mListener!=null) {
                        mListener.ensure();
                    }
                }
            });
        }


        View vcancel = findViewById(R.id.btn_cancel);
        if (vcancel!=null) {
            vcancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    if (mListener!=null) {
                        mListener.cancel();
                    }
                }
            });
        }

    }

    protected abstract int getlayoutId();

    protected void initContentViewBefor() {
        setCanceledOnTouchOutside(true);
        setCancelable(true);
    }

    public void setOnEnsureOrCancelListener(onEnsureOrCancelListener listener) {
        mListener = listener;
    }

    public interface onEnsureOrCancelListener{
        void ensure();

        void cancel();
    }

    private void setSystemUIVisible(boolean show) {
//        hideBottomUIMenu();
//        if (show) {
//            int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
//            uiFlags |= 0x00001000;
//            getWindow().getDecorView().setSystemUiVisibility(uiFlags);
//        } else {
//            int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
//            uiFlags |= 0x00001000;
//            getWindow().getDecorView().setSystemUiVisibility(uiFlags);
//        }
    }

    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }
}
